package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.DialogMapper;
import com.example.demo.pojo.Dialog;
import com.example.demo.pojo.User;
import com.example.demo.service.IDialogService;
import com.example.demo.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;

@Service
public class DialogServiceImpl extends ServiceImpl<DialogMapper, Dialog> implements IDialogService {

    @Autowired
    DialogMapper dialogMapper;


    @Override
    public List<Dialog> getDialogs(int id){
        List<Dialog> list=dialogMapper.getDialogs(id);
        Collections.sort(list,((o1, o2) -> Tools.getSeconds(o2.getSendDate(),o1.getSendDate())));
        return list;
    }

    public void insertDialog(User user,String content,int connection_id){
        Dialog dialog=new Dialog();
        Timestamp now=new Timestamp(System.currentTimeMillis());
        dialog.setHp_url(user.getHp_url());
        dialog.setUser_id(user.getUser_id());
        dialog.setUser_name(user.getUsername());
        dialog.setConnection_id(connection_id);
        dialog.setContent(content);
        dialog.setSendDate(now);
        dialogMapper.insert(dialog);
    }
}
